<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="球队城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="球队城市"></el-input>
    </el-form-item>
    <el-form-item label="球队老板" prop="boss">
      <el-input v-model="dataForm.boss" placeholder="球队老板"></el-input>
    </el-form-item>
    <el-form-item label="球队赛区" prop="division">
      <el-input v-model="dataForm.division" placeholder="球队赛区"></el-input>
    </el-form-item>
    <el-form-item label="球队创建时间" prop="created">
      <el-input v-model="dataForm.created" placeholder="球队创建时间"></el-input>
    </el-form-item>
    <el-form-item label="主教练" prop="coach">
      <el-input v-model="dataForm.coach" placeholder="主教练"></el-input>
    </el-form-item>
    <el-form-item label="球队简介" prop="introduction">
      <el-input v-model="dataForm.introduction" placeholder="球队简介"></el-input>
    </el-form-item>
    <el-form-item label="球队照片" prop="cover">
      <el-input v-model="dataForm.cover" placeholder="球队照片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          city: '',
          boss: '',
          division: '',
          created: '',
          coach: '',
          introduction: '',
          cover: ''
        },
        dataRule: {
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '球队城市不能为空', trigger: 'blur' }
          ],
          boss: [
            { required: true, message: '球队老板不能为空', trigger: 'blur' }
          ],
          division: [
            { required: true, message: '球队赛区不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '球队创建时间不能为空', trigger: 'blur' }
          ],
          coach: [
            { required: true, message: '主教练不能为空', trigger: 'blur' }
          ],
          introduction: [
            { required: true, message: '球队简介不能为空', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: '球队照片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/team/teaminformation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.teamInformation.name
                this.dataForm.city = data.teamInformation.city
                this.dataForm.boss = data.teamInformation.boss
                this.dataForm.division = data.teamInformation.division
                this.dataForm.created = data.teamInformation.created
                this.dataForm.coach = data.teamInformation.coach
                this.dataForm.introduction = data.teamInformation.introduction
                this.dataForm.cover = data.teamInformation.cover
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/team/teaminformation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'city': this.dataForm.city,
                'boss': this.dataForm.boss,
                'division': this.dataForm.division,
                'created': this.dataForm.created,
                'coach': this.dataForm.coach,
                'introduction': this.dataForm.introduction,
                'cover': this.dataForm.cover
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
